package com.jamal2362.apktool.view;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;

public class RoundedBackgroundSpan extends ReplacementSpan
{
	private final int _padding = 0;
	private int _backgroundColor;
	private int _textColor;

	public RoundedBackgroundSpan(int backgroundColor, int textColor)
	{
		super();
		_backgroundColor = backgroundColor;
		_textColor = textColor;
	}

	@Override
	public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm)
	{
		return (int) (_padding + paint.measureText(text.subSequence(start, end).toString()) + _padding);
	}

	@Override
	public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
	{
		float width = paint.measureText(text.subSequence(start, end).toString());
		RectF rect = new RectF(x - _padding, top, x + width + _padding, bottom);
		paint.setColor(_backgroundColor);
		canvas.drawRoundRect(rect, 10, 10, paint);
		paint.setColor(_textColor);
		canvas.drawText(text, start, end, x, y, paint);
	}
}
